package com.winning.sx.microframework.controller.sysmanage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.support.Ret;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.sysmanage.TableManageService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author wangj_envy
 * @date 2019年6月4日11:37:20
 */
@Controller
@RequestMapping(value = "tablemanage")
public class TableManageController {

    @Autowired
    private TableManageService tableManageService;

    @RequestMapping(value = {"/" , ""})
    public ModelAndView getTableInfos(){
        ModelAndView modelAndView = new ModelAndView("manage/table/tablemanage");
        return modelAndView;
    }
    @RequestMapping(value = "/table")
    public ModelAndView getTableInfos(String name) throws CustomException {
        Map<String,Object> map = new HashedMap();
        map.put("name",name);
        DataTable tableColumList = tableManageService.getTableColumList(map);
        ModelAndView modelAndView = new ModelAndView("manage/table/info");
        modelAndView.addObject("tableColumList",tableColumList.getDataRows());
        modelAndView.addObject("tableName",name);
        return modelAndView;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryAll"})
    public AjaxTableResponseMessage query() {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            DataTable table = tableManageService.getTableInfoList();
            ajaxTableResponseMessage.setDatas(table.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) table.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryData"})
    public AjaxTableResponseMessage queryData(String name,int start,int pageSize,String search) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            Map<String,Object> map = new HashedMap();
            map.put("table",name);
            map.put("pageNum",start);
            map.put("pageSize",pageSize);
            map.put("SearchKey",search);
            DataTable table = tableManageService.getTableData(map);
            ajaxTableResponseMessage.setDatas(table.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) table.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping("/save")
    public AjaxDataResponseMessage save(String datas,String tableName) {
        Map<String, Object> map = null;
        int i = 0;
        try {
            if (datas != null) {
                map = new ObjectMapper().readValue(datas, Map.class);
                map.put("tableName", tableName);
                i = tableManageService.saveTableData(map);
            }
        }catch (Exception e){
            return Ret.error("添加失败！请稍后重试！");
        }
        return Ret.success(datas);
    }

    @ResponseBody
    @RequestMapping("/update")
    public AjaxDataResponseMessage update(String datas,String tableName) {
        Map<String, Object> map = null;
        int i = 0;
        try {
            if (datas != null) {
                map = new ObjectMapper().readValue(datas, Map.class);
                map.put("tableName", tableName);
                i = tableManageService.updateTableData(map);
            }
        }catch (Exception e){
            return Ret.error("添加失败！请稍后重试！");
        }
        return Ret.success(datas);
    }

}
